package com.hrt.web.product.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.AddressBook;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.HRTMember;
import com.hrt.bean.OrderDetail;
import com.hrt.bean.ShoppingCar;
import com.hrt.web.addressBook.service.AddressBookService;
import com.hrt.web.member.service.HRTMemberService;
import com.hrt.web.product.service.GiftDetailService;
import com.hrt.web.product.service.GiftInforService;
import com.hrt.web.product.service.GiftTypeWebService;
import com.hrt.web.shoppingCar.service.ShoppingCarService;

@Controller
public class GiftInforControllorWeb extends BaseController<GiftInfor> {
	
	/**礼品基本信息*/
	@Autowired
	private GiftInforService giftInforService;
	@Autowired
	private GiftDetailService giftDetailService;
	@Autowired
	private HRTMemberService hrtMemberService;
	@Autowired
	private AddressBookService addressBookService;
	@Autowired
	private ShoppingCarService shoppingCarService;
	
	/**礼品分类*/
	@Autowired
	private GiftTypeWebService giftTypeWebService;
	
	/**
	 * 首页的倒叙排序前八条记录
	 * */
	@RequestMapping(value = "/index")
	public ModelAndView list(){
		ModelAndView mav = new ModelAndView();
		List<GiftInfor> list = this.giftInforService.searchofeight();
		mav.addObject("list",list);
		mav.setViewName("/index");
		return mav;
		
	}
	
	/**
	 * 分页
	 * *//*
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/HRTWEB/queryByPage2")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 12;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(giftInforService.totalCount());

		List<GiftInfor> resultList = giftInforService.queryByPage(getPageIndex(),
				getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	*//**
	 * 查看
	 * 
	 *//*
	@SuppressWarnings("null")
	@RequestMapping(value = "/HRTWEB/product_index")
	public ModelAndView productIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		setPageSize(12);
		list = giftInforService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(giftInforService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();
		
		List<GiftType> typeWebList = giftTypeWebService.findDelParents();
		if(typeWebList != null || typeWebList.size() > 0){
			for(GiftType type : typeWebList){
				List<GiftType> typeSonWebList = giftTypeWebService.findDelSonById(type.getId());
				type.setSonType(typeSonWebList);
			}
		}
		view.addObject("pageResult", page);
		view.addObject("parentsList", typeWebList);
//		view.addObject("sonList", typeSonWebList);
		return view;

	}*/
	
	/**
	 * 详情页面
	 * */
	@RequestMapping(value = "/HRTWEB/details/details")
	@ResponseBody
	public ModelAndView detailGI(
			@RequestParam(value = "id", required = true) String giId,
			HttpServletRequest request, HttpServletResponse response){
		ModelAndView view = new ModelAndView();
		GiftInfor gi = null;
		List<String> detailList = new ArrayList<String> ();
		if (giId != null && !"".equals(giId)) {
			gi = this.giftInforService.getById(new Long(giId));
			detailList = this.giftDetailService.getDetailIdforId(new Long(giId));
			byte[] buf = gi.getUeditorDetails();
			try {
				String strBuf = new String(buf, "UTF-8");
				
				gi.setDetails_Str(strBuf);
				
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		view.addObject("detailList", detailList);
		view.addObject("detailGI", gi);
		return view;
	}
	

	/**
	 * 查找
	 * 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/HRTWEB/find")
	@ResponseBody
	public Map findDetail(@RequestParam(value = "giftId", required = true) Long giftId,
			@RequestParam(value = "color", required = false) String colorName,
			HttpServletRequest request) {
		Map result = new HashMap();
		String color = null;
		try {
			color = new String(colorName.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<GiftDetail> gdDetail = this.giftDetailService.getColorAndGI(giftId, color.trim());
		result.put("gdDetail", gdDetail);
		return result;
		
		

	}
	/**
	 * 根据giftId查找礼品规格
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/HRTWEB/findAll")
	@ResponseBody
	public Map findAll(@RequestParam(value = "giftId", required = true) Long giftId,
			HttpServletRequest request) {
		Map result = new HashMap();
		
		List<GiftDetail> gdDetail = this.giftDetailService.getDetailByGI(giftId);
		result.put("gdDetail", gdDetail);
		return result;
		
		

	}
	/**
	 * 根据giftId查找礼品规格
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/HRTWEB/findByGdName")
	@ResponseBody
	public Map findByGdName(@RequestParam(value = "giftId", required = true) Long giftId,
			@RequestParam(value = "gdName", required = false) String gdName,
			HttpServletRequest request) {
		Map result = new HashMap();
		System.out.println("gdName---------->"+gdName);
		GiftDetail detail = this.giftDetailService.getByGiftIdAndName(giftId, gdName);
		result.put("detail", detail);
		return result;
		
		
	}
	/**
	 * 立即购买 vs 加入购物车
	 */
	@RequestMapping(value = "/HRTWEB/buyGift")
	@ResponseBody
	public ModelAndView buyGift(@RequestParam(value = "shopType", required = true) Long shopType,
			@RequestParam(value = "qty_item_1", required = false) Integer total,
			@RequestParam(value = "giftId", required = false) Long giftId,
			@RequestParam(value = "memberId", required = false) Long memberId,
			@RequestParam(value = "gdName", required = false) String gdName,
			HttpServletRequest request,HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		
		GiftDetail giftDetail = this.giftDetailService.getByGiftIdAndName(giftId, gdName);
		Double salePrice = giftDetail.getSalePrice();
		Double summery = total*salePrice;
		
		HRTMember hrtMember = hrtMemberService.getMemberByid(memberId);
		List<AddressBook> addressList = addressBookService.getAddListById(memberId);
		OrderDetail detail = new OrderDetail();
		detail.setGiftDetail(giftDetail);
		detail.setSalePrice(salePrice);
		detail.setTotal(total);
		detail.setSummery(summery);
		
		view.addObject("detail", detail);
		view.addObject("giftDetail", giftDetail);
		view.addObject("hrtMember", hrtMember);
		view.addObject("shopType", shopType);
		view.addObject("addressList", addressList);
		if(shopType.equals(new Long(1))){
			
			view.setViewName("/HRTWEB/buyGift");
		}
		else{
			
			ShoppingCar car = new ShoppingCar();
			car.setAddTime(shoppingCarService.getDate());//加入购物车时间
			car.setTotal(total);//礼品规格数量
			car.setSummery(summery);//订单金额
			car.setGiDetailId(giftDetail.getId());//礼品规格id
			car.setGiftId(giftDetail.getGiftId());//礼品id
			car.setGiftName(giftDetail.getGiftName());//礼品名称
			car.setGiDetailName(giftDetail.getGdName());//礼品规格名称
			car.setSalePrice(giftDetail.getSalePrice());//礼品规格单价
			car.setMemberId(memberId);
			car.setGiftDetail(giftDetail);
			car.setHrtMember(hrtMember);
			
			try {
				shoppingCarService.saveCar(car);
			} catch (Exception e) {
				e.printStackTrace();
			}
			List<ShoppingCar> carList = shoppingCarService.getShoppingCarByMemberId(memberId);
			view.addObject("carList", carList);
			view.setViewName("/HRTWEB/shoppingCar/shoppingCar");
			
		}
		return view;
		
		
	}
	

}
